<?php
/**
* @file Goodsm.php
* @synopsis  商品相关
* @author Yee, <rlk002@gmail.com>
* @version 1.0
* @date 2015年9月22日17:48:05
*/

defined('BASEPATH') OR exit('No direct script access allowed');

class Goodsm extends CI_Model
{
    const GOODS_TYPE = 1; //一般商品类别
    const BOOKS_TYPE = 2; //一般书籍类别
	function __construct()
	{
        parent::__construct();
	}
	//获取商品列表
	public function get_goods_list($start,$page_size,$goods_type=0)
	{
        $mc = $this->cache->memcached;
        $key = 'goods_list'.$goods_type.$start; //不同类别的缓存key不一样

        if(!$this->cache->memcached->is_supported())
        {
            $result = array("code" => -3,'tips' => "缓存未开启");
        }
        $data = $mc->get($key);
        if($data)
        {
            $return = $data;
        }
        else
        {
            //不是1.9.5版本 而是所有的版本都没有分页
            if($goods_type)
            {
                $ret = $this->db->select('id,goods_name,activity_info,goods_short_intro,jfpoints,goods_pics,author_name,real_money')->where(array("is_show"=>1,"goods_type"=>$goods_type))->order_by("sort")->order_by("jfpoints")->get("ml_goods")->result_array();
            }
            else
            {
                $ret = $this->db->select('id,goods_name,activity_info,goods_short_intro,jfpoints,goods_pics')->where("is_show",1)->order_by("sort")->order_by("jfpoints")->get("ml_goods")->result_array();
            }

            $return = [];
            foreach ($ret as $value) {
                $item = $value;
                $item['activity_info_new'] = $value['activity_info'];
                $item['activity_info'] = '';
                $item['goods_pics'] = $value['goods_pics'] ? DOWNLOAD_URL.$value['goods_pics'] : "";
                $item['goods_info_usrl'] = WEB_URL."goods/goods_info/".$value['id']."?version=".$this->softversion;
                $item['start_order_usrl'] = WEB_URL."goods/start_order/".$value['id']."?version=".$this->softversion;
                $return[] = $item;
            }
            $mc->save($key,$return, 60*10);
        }

        return $return;
	}

	//获取商品详情
	public function get_goods_info($goods_id)
	{
		$mc = $this->cache->memcached;
        $key = 'goods_info_'.$goods_id;

        if(!$this->cache->memcached->is_supported())
        {
            $result = array("code" => -3,'tips' => "缓存未开启");
        }
        $data = $mc->get($key);
        if($data)
        {
            $return = $data;
        }
        else
        {
        	$ret = $this->db->select('id,goods_name,goods_short_intro,jfpoints,goods_pics')->where("is_show",1)->where("id",$goods_id)->limit(1,0)->get("ml_goods")->result_array();
			if($ret)
			{
				$ret[0]['goods_pics'] = $ret[0]['goods_pics'] ? DOWNLOAD_URL.$ret[0]['goods_pics'] : "";
			}
			$return = $ret && $ret[0] ? $ret[0] : Null;
			$mc->save($key,$return, 60*10);
        }
		
		return $return;
	}

    /**
     * 获取书籍列表
     * @param $thread  用户所在科室
     */
    public function get_books_list($thread)
    {
        $mc = $this->cache->memcached;
        $key = 'goods_list'.$thread; //根据科室来区别缓存

        if(!$this->cache->memcached->is_supported())
        {
            $result = array("code" => -3,'tips' => "缓存未开启");
        }
        $data = $mc->get($key);
        if($data)
        {
            $return = $data;
        }
        else
        {
            //有科室就把科室的放到前面 否则就列出全部
            $thread_books_ids = '';
            if($thread)
            {
                $thread = $thread.',';
                $thread_ret = $this->db->select('id,goods_name,activity_info,goods_short_intro,jfpoints,goods_pics,author_name,real_money')->where(array("is_show"=>1,"goods_type"=>2))->like('typeid',$thread,'both')->order_by("sort")->order_by("jfpoints")->get("ml_goods")->result_array();
                foreach($thread_ret as $value)
                {
                    $thread_books_ids .= $value['id'].',';
                }
                $thread_books_ids = rtrim($thread_books_ids,',');
            }

            //取出不在科室里的  然后再合并
            if($thread_books_ids)
            {
                $no_thread_ret = $this->db->select('id,goods_name,activity_info,goods_short_intro,jfpoints,goods_pics,author_name,real_money')->where(array("is_show"=>1,"goods_type"=>2))->where_not_in('id',$thread_books_ids)->order_by("sort")->order_by("jfpoints")->get("ml_goods")->result_array();
            }
            else
            {
                $no_thread_ret = $this->db->select('id,goods_name,activity_info,goods_short_intro,jfpoints,goods_pics,author_name,real_money')->where(array("is_show"=>1,"goods_type"=>2))->order_by("sort")->order_by("jfpoints")->get("ml_goods")->result_array();
            }

            $ret = $thread_books_ids ? array_merge($thread_ret,$no_thread_ret) : $no_thread_ret;

            $return = [];
            foreach ($ret as $value) {
                $item = $value;
                $item['activity_info_new'] = $value['activity_info'];
                $item['activity_info'] = '';
                $item['goods_pics'] = $value['goods_pics'] ? DOWNLOAD_URL.$value['goods_pics'] : "";
                $item['goods_info_usrl'] = WEB_URL."goods/goods_info/".$value['id']."?version=".$this->softversion;
                $item['start_order_usrl'] = WEB_URL."goods/start_order/".$value['id']."?version=".$this->softversion;
                $return[] = $item;
            }
            $mc->save($key,$return, 60*10);
        }
        return $return;
    }
    /**
     * 获取分页书籍列表
     */
    public function get_books_list_2_2_2($thread,$start,$page_size)
    {
        $mc = $this->cache->memcached;
        $key = 'goods_list_'.$thread.'_'.$start; //根据科室来区别缓存
        if(!$this->cache->memcached->is_supported())
        {
            $result = array("code" => -3,'tips' => "缓存未开启");
        }
        $data = $mc->get($key);
        if($data = 0)
        {
            $return = $data;
        }
        else
        {
            if($thread)
            {
                $thread = $thread.',';
                $thread_ret = $this->db->select('id,goods_name,activity_info,goods_short_intro,jfpoints,goods_pics,author_name,real_money')->where(array("is_show"=>1,"goods_type"=>2))->like('typeid',$thread,'both')->order_by("convert(goods_name using gbk)",'asc')->get("ml_goods", $page_size, $start)->result_array();

            }
            else
            {
                $thread_ret = $this->db->select('id,goods_name,activity_info,goods_short_intro,jfpoints,goods_pics,author_name,real_money')->where(array("is_show"=>1,"goods_type"=>2))->order_by("convert(goods_name using gbk)",'asc')->get("ml_goods", $page_size, $start)->result_array();
            }
            $return = [];
            foreach ($thread_ret as $value) {
                $item = $value;
                $item['activity_info_new'] = $value['activity_info'];
                $item['activity_info'] = '';
                $item['goods_pics'] = $value['goods_pics'] ? DOWNLOAD_URL.$value['goods_pics'] : "";
                $item['goods_info_usrl'] = WEB_URL."goods/goods_info/".$value['id']."?version=".$this->softversion;
                $item['start_order_usrl'] = WEB_URL."goods/start_order/".$value['id']."?version=".$this->softversion;
                $return[] = $item;
            }
            $mc->save($key,$return, 60*10);
        }
        $thread = rtrim($thread,',');
        $thread = $thread?$thread:'all';
        $result = array('thread'=>$thread,'result'=>$return);
        return $result;
    }
    //判断科室下是否有书籍
    public function is_thread_book($thread)
    {
        if($thread == 'all')
        {
            return true;
        }
        $thread_ret = $this->db->select('id')->where(array("is_show"=>1,"goods_type"=>2))->like('typeid',$thread,'both')->order_by("sort")->order_by("jfpoints")->get("ml_goods")->row_array();
        return $thread_ret;
    }

}
